try:
    print(a)
except:
    print('error')

# 指定异常类型
try:
    print(a)
except NameError:
    print('error')

# 用元组指定多个异常类型
try:
    print(a)
except (NameError, TypeError):
    print('error')

# 获取异常信息
try:
    print(a)
except NameError as e:
    print('error', e)

# Exception可以捕获任意异常
try:
    print(a)
except Exception as e:
    print('error', e)

# 没有异常时，执行else中的代码
try:
    print('a')
except Exception as e:
    print('error', e)
else:
    print('ok')

# 无论是否有异常，都会执行finally中的代码
try:
    print(a)
except Exception as e:
    print('error', e)
else:
    print('ok')
finally:
    print('finally')

# 主动抛出异常
try:
    raise NameError('主动抛出的异常')
except NameError as e:
    print('error', e)
